package com.ln.property_control.controller;

import com.ln.property_control.pojo.*;
import com.ln.property_control.service.ScrapService;
import com.ln.property_control.utils.GetId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/scrap")
public class ScrapController {

    @Autowired
    private ScrapService scrapService;

    //资产报状态修显示
    @RequestMapping("/scrapFindAll")
    @ResponseBody
    public Map<String, Object> scrapFindAll(Integer page, Integer limit) {
        Map<String, Object> map = scrapService.scrapFindAll(page, limit);
        return map;
    }

    //所在组织下拉框
    @RequestMapping("/organisation")
    @ResponseBody
    public List organisation(Integer id) {
        List<Department> organisationList = scrapService.organisation(id);
        return organisationList;
    }

    //图片上传
    @PostMapping("/filesUpload")
    @ResponseBody
    public Map<String, Object> filesUpload(MultipartFile file) {
        String upload = scrapService.upload(file);
        HashMap<String, Object> map = new HashMap<>();
        map.put("url", upload);
        return map;
    }

    //提出申请维修
    @RequestMapping("/addMaintain")
    @ResponseBody
    public Map<String, Object> addMaintain(Scrap scrap,int[] faultId, Scrapphoto scrapphoto) {
        Map<String, Object> map = scrapService.insertScrap(scrap, faultId,scrapphoto);
        return map;
    }

    //维修申请流程审核
    @RequestMapping("/Processaudit")
    @ResponseBody
    public String Processaudit(ScrapVo scrapVo, Integer rankNum,String rname,String uname) {
        System.out.println(scrapVo);
        System.out.println(rankNum);
        /*判断当前计划审核时候已经完毕*/
        if (scrapVo.getApply_state() == 3 || scrapVo.getApply_state() == 4) {
            return "审批已结束！";
        } else {
            /*首先判断当前审批人员的级别与审批流程是否不符*/
            if (scrapVo.getApply_state() == 1) {//当前应当一级审核员审核
                if (rankNum ==1 ) {//当前审核员为一级人员
                    scrapVo.setApply_state(2);
                    scrapService.affirm(scrapVo);/*修改状态为等待二级审核*/

                    //将操作添加到日志中
                    Maintainhistory maintainhistory = new Maintainhistory();
                    maintainhistory.setScrap_id(scrapVo.getScrap_id());
                    maintainhistory.setApply_state(scrapVo.getApply_state());
                    maintainhistory.setResponsible(scrapVo.getResponsible());
                    maintainhistory.setScrap_operator(scrapVo.getScrap_operator());
                    maintainhistory.setAuditor(uname);
                    maintainhistory.setAuditrole(rname);
                    maintainhistory.setAudittime(new Date());
                    scrapService.insertHistory(maintainhistory);
                    return "审批成功！谢谢你的决策！";
                } else if (rankNum==2) {
                    scrapVo.setApply_state(4);
                    scrapService.affirm(scrapVo);/*修改状态为已通过*/
                    //将操作添加到日志中
                    Maintainhistory maintainhistory = new Maintainhistory();
                    maintainhistory.setScrap_id(scrapVo.getScrap_id());
                    maintainhistory.setApply_state(scrapVo.getApply_state());
                    maintainhistory.setResponsible(scrapVo.getResponsible());
                    maintainhistory.setScrap_operator(scrapVo.getScrap_operator());
                    maintainhistory.setAuditor(uname);
                    maintainhistory.setAuditrole(rname);
                    maintainhistory.setAudittime(new Date());
                    scrapService.insertHistory(maintainhistory);
                    return "审批成功！谢谢你的决策！";
                } else {
                    return "您无权操作，谢谢！";
                }
            } else if (scrapVo.getApply_state()== 2) {
                if (rankNum==1) {//当前审核员为一级人员
                    return "您已完成审批，请勿重复审批！";
                } else if (rankNum==2) {//当前审核员为二级人员
                    scrapVo.setApply_state(4);
                    scrapService.affirm(scrapVo);/*修改状态为已通过*/
                    //将操作添加到日志中
                    Maintainhistory maintainhistory = new Maintainhistory();
                    maintainhistory.setScrap_id(scrapVo.getScrap_id());
                    maintainhistory.setApply_state(scrapVo.getApply_state());
                    maintainhistory.setResponsible(scrapVo.getResponsible());
                    maintainhistory.setScrap_operator(scrapVo.getScrap_operator());
                    maintainhistory.setAuditor(uname);
                    maintainhistory.setAuditrole(rname);
                    maintainhistory.setAudittime(new Date());
                    scrapService.insertHistory(maintainhistory);
                    return "审批成功！谢谢你的决策！";
                } else {
                    return "您无权操作，谢谢！";
                }
            }
        }
        return "";
    }

    /*更改计划的通过状态 (回绝)*/
    @ResponseBody
    @RequestMapping("/refuse")
    public String refuse(ScrapVo scrapVo, Integer rankNum,String rname,String uname) {
        if(rankNum==1 || rankNum ==2){
            scrapVo.setApply_state(3);
            scrapService.affirm(scrapVo);
            //将操作添加到日志中
            Maintainhistory maintainhistory = new Maintainhistory();
            maintainhistory.setScrap_id(scrapVo.getScrap_id());
            maintainhistory.setApply_state(scrapVo.getApply_state());
            maintainhistory.setResponsible(scrapVo.getResponsible());
            maintainhistory.setScrap_operator(scrapVo.getScrap_operator());
            maintainhistory.setAuditor(uname);
            maintainhistory.setAuditrole(rname);
            maintainhistory.setAudittime(new Date());
            scrapService.insertHistory(maintainhistory);
            return "审批成功！谢谢你的决策！";
        }else{
            return "您无权操作谢谢";
        }
    }

    //查询资产是否存在
    @PostMapping("/exist")
    @ResponseBody
    public Map<String, Object> exist(String property_id) {
        Map<String, Object> map = scrapService.selectPropertyExist(property_id);
        return map;
    }

    //申请单的历史
    @RequestMapping("SelectHistory")
    @ResponseBody
    public Map<String, Object> SelectHistory(String scrap_id) {
        Map<String, Object> map= scrapService.SelectHistory(scrap_id);
        return map;
    }

    //修改资产状态
    @RequestMapping("updateScrapState")
    @ResponseBody
    public Map<String,Object> updateScrapState(Integer scrap_state,String scrap_id,String property_id){
        Map<String,Object> map=  scrapService.updateScrapState(scrap_state,scrap_id,property_id);
        return map;
    }

    //记录档案
    @RequestMapping("record")
    @ResponseBody
    public String record(Record record){
       String s= scrapService.insertRecord(record);
        return s;

    }

}

